package com.cn.xiaonuo.modular.utlis;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

import com.cn.xiaonuo.core.util.DateUtils;


@Component
public class IDGenerator {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 应大佬要求，随机数到6，时期年份去掉一半
     * 改redis生成唯一ID
     * @param prefix
     * @return
     */
    public  String generateCode(String prefix) {

        String currentDt = DateUtils.getCurrentDateStr(DateUtils.DATE_PARENT_YYMMDDHHMMSS_SSS);
       Long incrtNum=generate(RedisKeyContants.REDIS_ID_PREFIX+prefix,DateUtils.getTodayEndTime());
        if(null != incrtNum){
            return prefix+currentDt+"-"+incrtNum;
        }
        String random = RandomUtils.generateRandomNum(6);
        return prefix+currentDt+"-"+random;
    }

    public  String generateCode4NotSign(String prefix) {

        String currentDt = DateUtils.getCurrentDateStr(DateUtils.DATE_PARENT_YYMMDDHHMMSS_SSS);
        Long incrtNum=generate(RedisKeyContants.REDIS_ID_PREFIX+prefix,DateUtils.getTodayEndTime());
        if(null != incrtNum){
            return prefix+currentDt+incrtNum;
        }
        String random = RandomUtils.generateRandomNum(6);
        return prefix+currentDt+random;
    }

    public static String generate6Code(String prefix) {
        String currentDt = DateUtils.getCurrentDateStr(DateUtils.DATE_PATTERN_YYYYMMDDHHMMSS_SSS);


        String random = RandomUtils.generateRandomNum(6);
        return prefix+currentDt+"-"+random;
    }

    public  String generateTicketCode() {
        return DateUtils.formatDate(Calendar.getInstance().getTime(),
                "yyMMddHHmmSSS" + RandomUtils.generateRandomNum(3));
    }

    public static String generateShopCode(Integer num){
        // 0 代表前面补充0
        // 4 代表长度为4
        // d 代表参数为正数型
        return String.format("%03d", num);

    }

    public static String generateProductCategory(Integer num){
        return String.format("%02d", num);
    }

    public static String generateNewParentCategoryCode(String parentCode){
        if(parentCode.length()<4){
            return "CC"+parentCode;
        }else {
            return parentCode.replace(parentCode.substring(0,2),"CC");
        }


    }

    /**
     * @Title: set
     * @Description: set cache.
     * @param key
     * @param value
     * @param expireTime
     */
    public void set(String key,int value,Date expireTime) {
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        counter.set(value);
        counter.expireAt(expireTime);
    }

    /**
     * @Title: set
     * @Description: set cache.
     * @param key
     * @param value
     * @param timeout
     * @param unit
     */
    public void set(String key, int value, long timeout, TimeUnit unit) {
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        counter.set(value);
        counter.expire(timeout, unit);
    }

    /**
     * @Title: generate
     * @Description: Atomically increments by one the current value.
     * @param key
     * @return
     */
    public long generate(String key) {
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        return counter.incrementAndGet();
    }

    /**
     * @Title: generate
     * @Description: Atomically increments by one the current value.
     * @param key
     * @return
     */
    public  Long generate(String key, Date expireTime) {
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        counter.expireAt(expireTime);
        return counter.incrementAndGet();
    }

    /**
     * @Title: generate
     * @Description: Atomically adds the given value to the current value.
     * @param key
     * @param increment
     * @return
     */
    public long generate(String key,int increment) {
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        return counter.addAndGet(increment);
    }

    /**
     * @Title: generate
     * @Description: Atomically adds the given value to the current value.
     * @param key
     * @param increment
     * @param expireTime
     * @return
     */
    public long generate(String key,int increment,Date expireTime) {
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        counter.expireAt(expireTime);
        return counter.addAndGet(increment);
    }




    public static void main(String[] args) {

      /*  int num =Integer.parseInt("A5");
        System.out.println(num);*/
        String maxId="CC0101";
        String fisrt = maxId.substring(0,maxId.length()-2);
        String last2 = maxId.substring(maxId.length()-2) ;
        System.out.println(fisrt+23);

        System.out.println(generateNewParentCategoryCode("A0"));
      //  System.out.println(generateProductCategory(12));
    }
}
